Passed
Branch master (7d8670)
by Rafael S.
02:07 queued 21s
created

bit-packer.js ➔ fixByteArraySize   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 9.4285
1
/*
2
 * bit-packer: Pack and unpack nibbles, crumbs and booleans into bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
let pad = require("../src/byte-padding.js");
8
9
/**
10
 * Pack 2 nibbles in 1 byte.
11
 * @param {!Array<number>} nibbles Array of nibbles.
12
 * @return {!Array<number>} Pairs of neebles packed as one byte.
13
 */
14
function packNibbles(nibbles) {
15
    let packed = [];
16
    let i = 0;
17
    let j = 0;
18
    let len = nibbles.length;
19
    if (len % 2) {
20
        nibbles.push(0);
21
    }
22
    while (i < len) {
23
        packed[j++] = parseInt(
24
            nibbles[i].toString(16) + nibbles[i+1].toString(16), 16);
25
        i+=2;
26
    }
27
    return packed;
28
}
29
30
/**
31
 * Unpack a byte into 2 nibbles.
32
 * @param {!Array<number>|Uint8Array} bytes Array of bytes.
33
 * @return {!Array<number>} The nibbles.
34
 */
35
function unpackNibbles(bytes) {
36
    let unpacked = [];
37
    let i = 0;
38
    let j = 0;
39
    let len = bytes.length;
40
    while (i < len) {
41
        unpacked[j++] = parseInt(bytes[i].toString(16)[0], 16);
42
        unpacked[j++] = parseInt(bytes[i].toString(16)[1], 16);
43
        i++;
44
    }
45
    return unpacked;
46
}
47
48
/**
49
 * Pack 4 crumbs in 1 byte.
50
 * @param {!Array<number>} crumbs Array of crumbs.
51
 * @return {!Array<number>} 4 crumbs packed as one byte.
52
 */
53
function packCrumbs(crumbs) {
54
    let packed = [];
55
    let i = 0;
56
    let j = 0;
57
    pad.fixByteArraySize(crumbs, 4);
58
    let len = crumbs.length - 3;
59
    while (i < len) {
60
        packed[j++] = parseInt(
61
            pad.lPadZeros(crumbs[i].toString(2), 2) +
62
            pad.lPadZeros(crumbs[i+1].toString(2), 2) +
63
            pad.lPadZeros(crumbs[i+2].toString(2), 2) +
64
            pad.lPadZeros(crumbs[i+3].toString(2), 2), 2);
65
        i+=4;
66
    }
67
    return packed;
68
}
69
70
/**
71
 * Unpack a byte into 4 crumbs.
72
 * @param {!Array<number>|Uint8Array} crumbs Array of bytes.
73
 * @return {!Array<number>} The crumbs.
74
 */
75
function unpackCrumbs(crumbs) {
76
    let unpacked = [];
77
    let i = 0;
78
    let j = 0;
79
    let len = crumbs.length;
80
    let bitCrumb;
81
    console.log(len);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
    while (i < len) {
83
        bitCrumb = pad.lPadZeros(crumbs[i].toString(2), 8);
84
        unpacked[j++] = parseInt(bitCrumb[0] + bitCrumb[1], 2);
85
        unpacked[j++] = parseInt(bitCrumb[2] + bitCrumb[3], 2);
86
        unpacked[j++] = parseInt(bitCrumb[4] + bitCrumb[5], 2);
87
        unpacked[j++] = parseInt(bitCrumb[6] + bitCrumb[7], 2);
88
        i++;
89
    }
90
    return unpacked;
91
}
92
93
/**
94
 * Pack 8 booleans in 1 byte.
95
 * @param {!Array<number>} booleans Array of booleans.
96
 * @return {!Array<number>} 4 crumbs packed as one byte.
97
 */
98
function packBooleans(booleans) {
99
    let packed = [];
100
    let i = 0;
101
    let j = 0;
102
    pad.fixByteArraySize(booleans, 8);
103
    let len = booleans.length - 7;
104
    while (i < len) {
105
        packed[j++] = parseInt(
106
            booleans[i].toString(2) +
107
            booleans[i+1].toString(2) +
108
            booleans[i+2].toString(2) +
109
            booleans[i+3].toString(2) +
110
            booleans[i+4].toString(2) +
111
            booleans[i+5].toString(2) +
112
            booleans[i+6].toString(2) +
113
            booleans[i+7].toString(2), 2);
114
        i+=8;
115
    }
116
    return packed;
117
}
118
119
/**
120
 * Unpack a byte into 8 booleans.
121
 * @param {!Array<number>|Uint8Array} booleans Array of bytes.
122
 * @return {!Array<number>} The booleans.
123
 */
124
function unpackBooleans(booleans) {
125
    let unpacked = [];
126
    let i = 0;
127
    let j = 0;
128
    let len = booleans.length;
129
    let bitBoolean;
130
    while (i < len) {
131
        bitBoolean = pad.lPadZeros(booleans[i].toString(2), 8);
132
        unpacked[j++] = parseInt(bitBoolean[0], 2);
133
        unpacked[j++] = parseInt(bitBoolean[1], 2);
134
        unpacked[j++] = parseInt(bitBoolean[2], 2);
135
        unpacked[j++] = parseInt(bitBoolean[3], 2);
136
        unpacked[j++] = parseInt(bitBoolean[4], 2);
137
        unpacked[j++] = parseInt(bitBoolean[5], 2);
138
        unpacked[j++] = parseInt(bitBoolean[6], 2);
139
        unpacked[j++] = parseInt(bitBoolean[7], 2);
140
        i++;
141
    }
142
    return unpacked;
143
}
144
145
module.exports.packBooleans = packBooleans;
146
module.exports.unpackBooleans = unpackBooleans;
147
module.exports.packCrumbs = packCrumbs;
148
module.exports.unpackCrumbs = unpackCrumbs;
149
module.exports.packNibbles = packNibbles;
150
module.exports.unpackNibbles = unpackNibbles;
151